package xin.jiangqiang;

import xin.jiangqiang.util.PropertiesUtil;

import java.util.Arrays;

/**
 * @author JiangQiang
 * @date 2019/4/7 15:39
 */
public class AppRun {
    public static void main(String[] args) {
        String cycle = PropertiesUtil.getProperty("Cycle");
        if (cycle != null) {
            String[] cyc = cycle.split(":");
            int hour = 0;
            int minute = 0;
            if (cyc.length == 2) {
                hour = Integer.parseInt(cyc[0]);
                minute = Integer.parseInt(cyc[1]);
            } else if (cyc.length == 1) {
                minute = Integer.parseInt(cyc[0]);
            } else {
                run();
                return;
            }
            System.out.println("执行周期为：" + (hour * 60 + minute) + "分钟");
            for (int i = 1; ; ) {
                try {
                    Thread.sleep((hour * 60 + minute) *60* 1000);
                    System.out.println("第" + i + "次执行");
                    run();
                    i++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } else {/*如果没有配置每隔多长时间运行，则默认只执行一次*/
            run();
        }
    }

    public static void run() {
        System.out.println("开始ddns检查");
        UpdateDomainRecord record = new UpdateDomainRecord();
        record.analysisDns();
        System.out.println("ddns运行结束");
    }
}
